package invest.data.getdata;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import invest.data.Data;
import invest.pojo.XueqiuCookie;
import invest.pojo.datapojo.StockMinuteToday;
import invest.utils.HttpRequest;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StockTodyTimeData implements Data<List<StockMinuteToday>> {
    private String code = "";
    //    private String url = "https://stock.xueqiu.com/v5/stock/chart/kline.json?symbol=SZ128033&begin=1617021934480&period=day&type=before&count=-100000&indicator=kline,pe,pb,ps,pcf,market_capital,agt,ggt,balance";
    private String pre = "https://stock.xueqiu.com/v5/stock/chart/minute.json?symbol=";
    private String after = "&period=1d";

    public StockTodyTimeData(String code) {
        boolean flag = false;
        Pattern p = Pattern.compile(".*\\d+.*");
        Matcher m = p.matcher(code);
        if (m.matches()) {
            flag = true;
        }
        if (flag) {
            if (code.length() <= 5) {
                this.code = code;
            } else {
                if (code.startsWith("6")) {
                    this.code = "SH" + code;
                } else {
                    this.code = "SZ" + code;
                }
            }
        } else {
            this.code = code;
        }

    }

    @Override
    public List<StockMinuteToday> getData() throws IOException {
        String cookie = XueqiuCookie.getXueqiuCookie();
        Long timeMillis = System.currentTimeMillis(); // 当前时间戳
        String url = pre + code + after;
        String result = HttpRequest.sendGet(url, cookie);
        JSONObject jb = JSONObject.parseObject(result);
        JSONObject data = jb.getJSONObject("data");
        JSONArray item = data.getJSONArray("items");
        List<StockMinuteToday> stockMinuteTodays = new ArrayList<>();
        for (int i = 0; i < item.size(); i++) {
            JSONObject jsonObject = item.getJSONObject(i);
            StockMinuteToday stockToday = new StockMinuteToday();
            stockToday.setTime(new Date((Long) jsonObject.get("timestamp")));
            stockToday.setVolume((Integer) jsonObject.get("volume"));
            stockToday.setPrice(new Float(((BigDecimal) jsonObject.get("current")).doubleValue()));
            if (jsonObject.get("percent") != null) {
                stockToday.setPercent(new Float(((BigDecimal) jsonObject.get("percent")).doubleValue()));
            }
            stockToday.setAmount(new Double(((BigDecimal) jsonObject.get("amount")).doubleValue()));
            if (jsonObject.get("chg") != null) {
                stockToday.setChg(new Float(((BigDecimal) jsonObject.get("chg")).doubleValue()));
            }
            stockMinuteTodays.add(stockToday);
        }
        return stockMinuteTodays;
    }

    public static void main(String[] args) throws IOException {
        StockTodyTimeData stockTodyTimeData = new StockTodyTimeData("301381");
        List<StockMinuteToday> stockMinuteTodays = stockTodyTimeData.getData();
        System.out.println(1);

    }
}
